/*
This file is part of SmartShopper

SmartShopper is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

SmartShopper is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with vlc-remote.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <QtSql>
#include "databaseconnect.h"


DatabaseConnect::DatabaseConnect(QString dataPath, QObject *parent) :
    QObject(parent)
{
    mDataPath = dataPath + "sshop.db";
    mDb = QSqlDatabase::addDatabase("QSQLITE");
    mDb.setDatabaseName(mDataPath);
    if (!(checkDbOpen()))
        qApp->quit();

    QStringList tablesList = mDb.tables();
    if (!(tablesList.contains("magazin")))
        createDatabseFile();
}

void DatabaseConnect::createDatabseFile()
{
    // here we can add message for First Program start

    QSqlQuery sql;

//! [Set up the category type table]
    sql.exec("create table category (id int primary key, name varchar(20))");
//    sql.exec("insert into category values(0, '')");    // empty row
    // some rows for test
    sql.exec("insert into category values(1, 'Food')");
    sql.exec("insert into category values(2, 'Maintenance')");
    sql.exec("insert into category values(3, 'Universal')");
//! [Set up the category type table]

//! [Set up the magazin table]
    sql.exec("create table magazin (id int primary key, "
               "name varchar(50), categoryid int, gpsid int)");
//    sql.exec("insert into magazin values(0, '', 0, null)");    // empty row
    // some rows for test
    sql.exec("insert into magazin values(1, 'Magnit', "
               "1, null)");
    sql.exec("insert into magazin values(2, 'Line', "
               "3, null)");
//! [Set up the magazin table]

//! [Set up the ShopList type table]
    sql.exec("create table shoplist (id int primary key, name varchar(20),"
               "magazinid int, state int)");
//    sql.exec("insert into shoplist values(0, '', 0, 0)");
    sql.exec("insert into shoplist values(1, 'DayFood', 1, 1)");
    sql.exec("insert into shoplist values(2, 'Chemicals', 2, 1)");
//    sql.exec("create table shoplist (id int primary key, name varchar(20),"
//               "magazinid int, categoryid int)");
//    sql.exec("insert into shoplist values(1, 'DayFood', 1, 1)");
//    sql.exec("insert into shoplist values(2, 'Chemicals', 2, 3)");
//! [Set up the ShopList type table]

//! [Set up the purchase type table]
    sql.exec("create table purchase (id int primary key, name varchar(20),"
               "shoplistid int, amount int, checkstate int)");
//    sql.exec("insert into purchase values(0, '', 0, 0, 0)");
    // some rows for test
//    sql.exec("insert into purchase values(1, 'Bread', 1, 0, 0)");
//    sql.exec("insert into purchase values(2, 'Meat', 1, 2, 0)");
//    sql.exec("insert into purchase values(3, 'Milk', 1, 3, 0)");

//    sql.exec("insert into purchase values(4, 'Domestos', 2, 1, 0)");
//    sql.exec("insert into purchase values(5, 'Tide', 2, 1, 0)");
//! [Set up the purchase type table]

//! [Set up the Purchase Reference type table]
    sql.exec("create table purref (id int primary key, name varchar(20),"
               "int categoryid)");
//    sql.exec("insert into purref values(0, '', 0)");
    // some rows for test
//    sql.exec("insert into purref values(1, 'Bread', 1)");
//    sql.exec("insert into purref values(2, 'Domestos', 2)");
//! [Set up the Purchase Reference type table]

//! [Set up the GPS coords table]
    sql.exec("create table gpscoord (id int primary key, latitude float,"
               "longitude float, altitude float)");
//! [Set up the GPS coords table]

//! [Set up the Reminders table]
    sql.exec("create table reminders (id int primary key, name varchar(20), alarm datetime)");
//! [Set up the Reminders table]
}

bool DatabaseConnect::checkDbOpen()
{
    if (!(mDb.open())) {
#if defined(Q_WS_MAEMO_5)
        QMaemo5InformationBox::information(this,tr("Can't open database"),
                                           QMaemo5InformationBox::DefaultTimeout);
#else
        //TODO: emit error signal for QML components can display message box
        qDebug() << "Cannot open database";
//        QMessageBox::critical(0, tr("Cannot open database"),
//            tr("Unable to establish a database connection.\n"
//                     "Click Cancel to exit."), QMessageBox::Cancel);
#endif
        return false;
    }
    return true;
}
